package com.alfred.java.customannotation;

import com.alfred.java.customannotation.annotation.MyComponent;
import com.alfred.java.customannotation.annotation.MyService;
import com.alfred.java.customannotation.service.UserService;

import javax.annotation.*;
import java.lang.annotation.*;

/**
 * @Author: alfred
 * @Date: 2021/1/28
 * @description:
 */
public class Main {

    public static void main(String[] args) {
        Class<UserService> classz = UserService.class;
        getAnnos(classz);
    }

    /**
     * interface java.lang.annotation.Documented 等 存在循环，导致内存溢出，所以需要排除java的源注解
     * @param classz
     */
    private static void getAnnos(Class<?> classz){
        Annotation[] annotations = classz.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType() != Deprecated.class &&
                    annotation.annotationType() != SuppressWarnings.class &&
                    annotation.annotationType() != Override.class &&
                    annotation.annotationType() != PostConstruct.class &&
                    annotation.annotationType() != PreDestroy.class &&
                    annotation.annotationType() != Resource.class &&
                    annotation.annotationType() != Resources.class &&
                    annotation.annotationType() != Generated.class &&
                    annotation.annotationType() != Target.class &&
                    annotation.annotationType() != Retention.class &&
                    annotation.annotationType() != Documented.class &&
                    annotation.annotationType() != Inherited.class
            ) {
                if (annotation.annotationType() == MyComponent.class){
                    String name = ((MyComponent) annotation).name();
                    System.out.println("MyComponent name:"+name);
                }else if (annotation.annotationType() == MyService.class){
                    String say = ((MyService) annotation).say();
                    String hello = ((MyService) annotation).hello();
                    System.out.println("MyService say:"+say+"\thello:"+hello);
                }
                getAnnos(annotation.annotationType());
            }
        }
    }

}
